Imports System.Management

Namespace Web

  ''' <summary>
  ''' Determines what action to take on the web server.
  ''' </summary>
  ''' <remarks></remarks>
  Public Enum IisWebServiceAction
    GetCurrentMode
    PauseService
    ResumeService
    StartService
    StopService
  End Enum

  Public NotInheritable Class IisServerHelper
    Const STR_W3SVC As String = "W3SVC"

    ''' <summary>
    ''' Alows actions to be taken on a web server.
    ''' </summary>
    ''' <param name="server">Server name.</param>
    ''' <param name="userName">User name. Required for remote server.</param>
    ''' <param name="password">User password. Required for remote server.</param>
    ''' <param name="domain">Optional domain name.</param>
    ''' <param name="instance">Web server instance. Optional, W3SVC will be used if not given.</param>
    ''' <param name="action">Action to take on the web server.</param>
    ''' <returns>Return value from call to the web server.</returns>
    ''' <remarks></remarks>
    Public Shared Function ManageIisWebService(ByVal server As String, ByVal userName As String, ByVal password As System.Security.SecureString, ByVal domain As String, ByVal instance As String, ByVal action As IisWebServiceAction) As Int32
      Dim result As Int32

      If String.IsNullOrEmpty(server) Then
        Throw New ArgumentNullException("server")
      End If

      'Verify required parameters
      If Not IisWebServiceAction.IsDefined(GetType(IisWebServiceAction), action) Then
        Throw New ArgumentOutOfRangeException("action")
      End If

      'default to the local system
      Dim path As String = String.Format(CultureInfo.CurrentCulture, "\\{0}\root\MicrosoftIISv2", server)
      Dim connection As New ConnectionOptions

      If Not String.IsNullOrEmpty(userName) Then
        connection.Username = userName
      End If

      If (Not IsNothing(password)) AndAlso (Not String.IsNullOrEmpty(password.ToString)) Then
        connection.Password = password.ToString
      End If


      If Not String.IsNullOrEmpty(domain) Then
        connection.Authority = String.Format(CultureInfo.CurrentCulture, "ntlmdomain:{0}", domain)
      End If

      connection.Impersonation = ImpersonationLevel.Impersonate
      connection.Authentication = AuthenticationLevel.Call


      'Create the conneciton and connect
      Dim scope As New ManagementScope(path, connection)
      scope.Connect()

      'Set instance
      If String.IsNullOrEmpty(instance) Then
        instance = STR_W3SVC
      End If

      'Create the ManagementObject
      Using classInstance As New ManagementObject(scope, New ManagementPath(String.Format(CultureInfo.CurrentCulture, "IIsWebService.Name='{0}'", instance)), Nothing)

        'Run the command
        Dim outParams As ManagementBaseObject = classInstance.InvokeMethod(action.ToString, Nothing, Nothing)

        If Not Int32.TryParse(outParams("result").ToString, result) Then
          result = -1
        End If

        outParams.Dispose()

      End Using

      Return result

    End Function

    Private Sub New()
    End Sub

  End Class
End Namespace